package com.atwisdom.star.core.dao.pojo;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 
 * @TableName event
 */
@TableName(value ="event",autoResultMap = true)
@Data
public class Event  extends BaseEntity  {
    /**
     * 事件标识
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 事件分类
     */
    @TableField(value = "category")
    private String category;

    /**
     * 事件名称
     */
    @TableField(value = "name")
    private String name;

    /**
     * 事件信息
     */
    @TableField(value = "event",typeHandler = FastjsonTypeHandler.class)
    private JSONObject event;

    /**
     * 版本号
     */
    @TableField(value = "version")
    private BigDecimal version;

    /**
     * 是否发布
     */
    @TableField(value = "published")
    private Boolean published;

    /**
     * 说明
     */
    @TableField(value = "comments")
    private String comments;

    /**
     * 逻辑删除
     */
    @TableField(value = "deleteFlag")
    private Boolean deleteFlag;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public Event(){

    }

    /**
     * 有参构造方法
     *
     * @param jsonObject 参数
     */
    public Event(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getLong("id") : null;
        this.category = keys.contains("category") ? jsonObject.getString("category") : null;
        this.name = jsonObject.getString("name");
        this.event = keys.contains("event") ? jsonObject.getJSONObject("event"):null;
        this.version = keys.contains("version") ? jsonObject.getBigDecimal("version") : BigDecimal.valueOf(1.0);
        this.published = keys.contains("published") ? jsonObject.getBoolean("published") : true;
        this.deleteFlag = keys.contains("deleteFlag") ? jsonObject.getBoolean("deleteFlag") : false;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
    }

    /**
     * 将当前模型对象转成jsonObject
     *
     * @return 返回结果
     */
    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.id);
        JsonObject.put("category", this.category);
        JsonObject.put("name", this.name);
        JsonObject.put("event", this.event);
        JsonObject.put("version", this.version);
        JsonObject.put("published", this.published);
        JsonObject.put("deleteFlag", this.deleteFlag);
        JsonObject.put("comments", this.comments);
        JsonObject.put("createdBy", this.createdBy);
        JsonObject.put("createTime", this.createTime);
        JsonObject.put("updatedBy", this.updatedBy);
        JsonObject.put("updateTime", this.updateTime);
        return JsonObject;
    }

    /**
     * 获取摘要数据列
     *
     * @return 反回查询列
     */
    public static String[] fetchSimpleColumns() {
        List<String> columns = new ArrayList<>();
        columns.add("id");
        columns.add("category");
        columns.add("name");
        columns.add("version");
        columns.add("published");
        columns.add("deleteFlag");
        columns.add("updatedBy");
        columns.add("updateTime");
        columns.add("createdBy");
        columns.add("createTime");
        return columns.toArray(new String[0]);
    }
}